package cn.zyx.controller;


import cn.zyx.Config.Result;
import cn.zyx.entity.Credit;
import cn.zyx.service.CreditService;
import com.alibaba.fastjson.JSON;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Credit)表控制层
 *
 * @author makejava
 * @since 2022-01-03 16:10:41
 */
@RestController
@RequestMapping("credit")
public class CreditController {

    /**
     * 服务对象
     */
    @Resource
    private CreditService creditService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Credit selectOne(Integer id) {
        return this.creditService.queryById(id);
    }

    /**
     * 话费接口
     * @return
     */
    @RequestMapping("selopip")
    public  Map selopip(){
        List<Credit> credits = this.creditService.queryByOp(0);
        List<Credit> credits1 = this.creditService.queryByOp(1);
        List<Credit> credits2 = this.creditService.queryByOp(2);
        Map<String,Object> map =new HashMap<>();
        map.put("移动",credits);
        map.put("电信",credits1);
        map.put("联通",credits2);
        ValueOperations ops = redisTemplate.opsForValue();
        String s = JSON.toJSONString(map);
        System.out.println(s);
        ops.set("data",s);
        return map;
    }

    /**
     * 电费接口
     * @return
     */
    @RequestMapping("selopd")
    public  Map selopd(){
        List<Credit> credit3 = this.creditService.queryByOp(3);
        Map<String,Object> map =new HashMap<>();
        map.put("电费",credit3);
        ValueOperations ops = redisTemplate.opsForValue();
        String s = JSON.toJSONString(map);
        System.out.println(s);
        ops.set("data",s);
        return map;
    }
    /**
     * 会员接口
     * @return
     */
    @RequestMapping("seloph")
    public  Map seloph(){
        List<Credit> credit5 = this.creditService.queryByOp(5);
        Map<String,Object> map =new HashMap<>();
        map.put("会员",credit5.get(0));
        ValueOperations ops = redisTemplate.opsForValue();
        String s = JSON.toJSONString(map);
        System.out.println(s);
        ops.set("data",s);
        return map;
    }
    /**
     * 油卡接口
     * @return
     */
    @RequestMapping("selopy")
    public  Map selopy(){
        List<Credit> credit4 = this.creditService.queryByOp(4);
        Map<String,Object> map =new HashMap<>();
        map.put("油卡",credit4);
        ValueOperations ops = redisTemplate.opsForValue();
        String s = JSON.toJSONString(map);
        System.out.println(s);
        ops.set("data",s);
        return map;
    }




    /**
     * 查询全部
     * @param page 分页
     * @param limit
     * @param operator 条件
     * @return
     */
    @RequestMapping("selall")
    public Result queryAll(int page, int limit, Integer operator){
        int offset=(page-1)*limit;
        return this.creditService.queryAllByLimit(offset,limit,operator);
    }

    /**
     * 添加
     * @param credit
     * @return
     */
    @RequestMapping("add")
    public Result insert(Credit credit){
        Credit insert = creditService.insert(credit);
        Result result=new Result(0,"成功",insert,1);
        return result;
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @RequestMapping("del")
    public Result del(Integer id){
        boolean b = creditService.deleteById(id);
        Result result=new Result(0,"成功",b,1);
        return result;
    }

    /**
     * 修改
     * @param credit
     * @return
     */
    @RequestMapping("update")
    public Result update(Credit credit){
        Credit update = creditService.update(credit);
        Result result=new Result(0,"成功",update,1);
        return result;
    }


}